package com.lims.admin.dao;

import java.util.List;

import javacommon.base.*;

import cn.org.rapid_framework.page.*;

import com.lims.admin.model.*;
import com.lims.admin.vo.query.*;

import org.springframework.stereotype.Repository;

@Repository @SuppressWarnings({"unchecked"})
public class UserDao extends BaseHibernateDao<User,java.lang.String>{

	public Class getEntityClass() {
		return User.class;
	}
	
	public Page findPage(UserQuery query) {
        //XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接 
        // [column] 为PageRequest的属性
		String sql = "select t from User t where 1=1 "
			  	+ "/~ and t.userno = {userno} ~/"
			  	+ "/~ and t.userName = {userName} ~/"
			  	+ "/~ and t.password = {password} ~/"
			  	+ "/~ and t.userRealName = {userRealName} ~/"
				+ "/~ order by [sortColumns] ~/";
        
		return pageQuery(sql,query);
	}

	public List<Role> findRoleByUserId(String uids) {
		String hql = "select u.roles from User as u where u.objid='"+ uids+"'";
		return this.getHibernateTemplate().find(hql);
	}

	public List findMenusByUser(User user) {
		String hql = "select m from User u " +
				" inner join u.roles as r " +
				" inner join r.menus as m" +
				" left join fetch m.parentMenu cm" +
				" where u.objid='"+user.getObjid()+"' and m.menulevel=2";
		List list = this.getHibernateTemplate().find(hql);
		return list;
	}

}
